package com.fingard.dsp.bank.directbank.jrtt02;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.bytedance.ea.bankconnector.api.AccountQueryService;
import com.bytedance.ea.bankconnector.api.PaymentExecuteService;
import com.bytedance.ea.bankconnector.api.PaymentQueryService;
import com.bytedance.ea.rulevalidator.api.PaymentInfoVerifyService;
import com.bytedance.ea.rulevalidator.api.PaymentRuleBasicService;
import com.bytedance.ea.rulevalidator.api.PaymentRuleDetailService;
import com.bytedance.ea.rulevalidator.api.RuleVersionService;
import com.fingard.dsp.bank.directConfig.BankConfig;
import com.fingard.dsp.bankset.BankFront;

public class DubboBeanFactory extends JRTT02Base{

    ReferenceConfig<RuleVersionService.Iface> ruleVersionService;
    ReferenceConfig<PaymentRuleBasicService.Iface> basicService;
    ReferenceConfig<PaymentRuleDetailService.Iface> paymentRuleDetailService;
    ReferenceConfig<PaymentInfoVerifyService.Iface> payVerifyService;
    ReferenceConfig<AccountQueryService.Iface> actQueryService;
    ReferenceConfig<PaymentExecuteService.Iface> payService;
    ReferenceConfig<PaymentQueryService.Iface> payQueryService;

    private static ApplicationConfig config ;

    private static RegistryConfig reg;

    public DubboBeanFactory(BankFront bankFront, BankConfig bankConfig){
        config = new ApplicationConfig(bankConfig.getValueAsString("applicationConfig"));
        reg = new RegistryConfig(bankFront.serverIP+":"+bankFront.serverPort);
        reg.setProtocol(bankConfig.getValueAsString("protocol"));
    }

    /**
     * 版本信息查询
     * @return
     */
    public RuleVersionService.Iface getRuleVersionService() {
        if (ruleVersionService == null) {
            ruleVersionService = new ReferenceConfig<RuleVersionService.Iface>();
            ruleVersionService.setApplication(config);
            ruleVersionService.setRegistry(reg);
            ruleVersionService.setInterface(RuleVersionService.Iface.class);
            ruleVersionService.setTimeout(30000);
            ruleVersionService.setRetries(3);
            ruleVersionService.setCheck(true);
        }
        return ruleVersionService.get();
    }

    /**
     * 基本付款信息查询
     * @return
     */
    public PaymentRuleBasicService.Iface getBasicService() {
        if (basicService == null) {
            basicService = new ReferenceConfig<PaymentRuleBasicService.Iface>();
            basicService.setApplication(config);
            basicService.setRegistry(reg);
            basicService.setInterface(PaymentRuleBasicService.Iface.class);
            basicService.setTimeout(30000);
            basicService.setRetries(3);
            basicService.setCheck(true);
        }
        return basicService.get();
    }

    /**
     * 详细付款信息查询
     * @return
     */
    public PaymentRuleDetailService.Iface getDetailService() {
        if (paymentRuleDetailService == null) {
            paymentRuleDetailService = new ReferenceConfig<PaymentRuleDetailService.Iface>();
            paymentRuleDetailService.setApplication(config);
            paymentRuleDetailService.setRegistry(reg);
            paymentRuleDetailService.setInterface(PaymentRuleDetailService.Iface.class);
            paymentRuleDetailService.setTimeout(30000);
            paymentRuleDetailService.setRetries(3);
            paymentRuleDetailService.setCheck(true);
        }
        return paymentRuleDetailService.get();
    }

    /**
     * 付款信息预检接口
     * @return
     */
    public PaymentInfoVerifyService.Iface getVerifyService() {
        if (payVerifyService == null) {
            payVerifyService = new ReferenceConfig<PaymentInfoVerifyService.Iface>();
            payVerifyService.setApplication(config);
            payVerifyService.setRegistry(reg);
            payVerifyService.setInterface(PaymentInfoVerifyService.Iface.class);
            payVerifyService.setTimeout(30000);
            payVerifyService.setRetries(3);
            payVerifyService.setCheck(true);
        }
        return payVerifyService.get();
    }

    /**
     * 账户信息查询接口
     * @return
     */
    public AccountQueryService.Iface getAccountService() {
        if (actQueryService == null) {
            actQueryService = new ReferenceConfig<AccountQueryService.Iface>();
            actQueryService.setApplication(config);
            actQueryService.setRegistry(reg);
            actQueryService.setInterface(AccountQueryService.Iface.class);
            actQueryService.setTimeout(30000);
            actQueryService.setRetries(3);
            actQueryService.setCheck(true);
        }
        return actQueryService.get();
    }

    /**
     * 付款接口
     * @return
     */
    public PaymentExecuteService.Iface getPayService() {
        if (payService == null) {
            payService = new ReferenceConfig<PaymentExecuteService.Iface>();
            payService.setApplication(config);
            payService.setRegistry(reg);
            payService.setInterface(PaymentExecuteService.Iface.class);
            payService.setTimeout(30000);
            payService.setRetries(3);
            payService.setCheck(true);
        }
        return payService.get();
    }

    /**
     * 付款信息查询接口
     * @return
     */
    public PaymentQueryService.Iface getPayQueryService() {
        if (payQueryService == null) {
            payQueryService = new ReferenceConfig<PaymentQueryService.Iface>();
            payQueryService.setApplication(config);
            payQueryService.setRegistry(reg);
            payQueryService.setInterface(PaymentQueryService.Iface.class);
            payQueryService.setTimeout(30000);
            payQueryService.setRetries(3);
            payQueryService.setCheck(true);
        }
        return payQueryService.get();
    }
}
